#!/usr/local/bin/bash

first_msg()
{
    echo "***********************************************************"
    echo "  "
    echo "The batch process BACKUP_DAILY is starting at [ 'date' ]"
    echo "Excuting by LogName = [ $LOGNAME ]"
    echo "  "
}

final_msg()
{
    echo "  "
    echo "The batch process BACKUP_DAILY completed at [ 'date' ]"
    echo "Excuting by LogName = [ $LOGNAME ]"
    echo ">>>>> Please Make Sure That It Has Been Done Successfully, "
    echo ">>>>> Otherwise You Must Re-do It."
    echo "  "
    echo "***********************************************************"
}

#********************************
#             Main              *
#********************************
USER_PASSWD=system/system
ARCHIVE_DEST=/appl/oracle/oradata/orafe/arch
DEVICE=/dev/rmt/ctape1
TRC_FILE=$TRC/Z_JOB_BACKUP_DAILY
TMP_SQL=$TRC/backup_daily_tmp.sql
BACKUP_DEST=${BACKUP_PATH}/$(date '+%Y%m%d')
SUCCESS=0
FAIL=1

first_msg
mkdir -m 777 -p $BACKUP_DEST
if [ ! -d $BACKUP_DEST ]
then
    echo "$BACKUP_DEST create fail"
    exit
fi

#*******************************
#      backup archive log      *
#*******************************
echo "==========================================================="
echo "Backup archive log begin on [ 'date' ]"
echo "alter system archive log stop;" > $TMP_SQL
sqlplus -s $USER_PASSWD <<EOF
start $TMP_SQL
EOF
if [ $? != 0 ]
then
    echo "alter archive log stop fail"
    exit
fi
cd $ARCHIVE_DEST
FILE_QT='ls|grep -c "arch*"'
FILE='ls|grep "arch*"'

echo "alter system archive log start;" > $TMP_SQL
sqlplus -s $USER_PASSWD <<EOF
start $TMP_SQL
EOF
if [ $? != 0 ]
then
    echo "alter archive log start fail"
    exit
fi
if [ $FILE_QT != 0 ]
then 
    mv -f $FILE $BACKUP_DEST
    cd $BACKUP_DEST
    compress -f *
    tar -cvf $DEVICE *
    if [ $? != 0 ]
    then
        echo "tar to tape fail"
        exit
    fi
fi
echo "Backup archive log end on [ 'date' ]"
echo "==========================================================="
final_msg
